
import org.codehaus.groovy.grails.commons.ApplicationHolder

import com.medranocg.gd.Role
import com.medranocg.gd.Account
import com.medranocg.gd.AccountRole
import com.medranocg.gd.FileType
import com.medranocg.gd.DocumentType
import com.medranocg.gd.DocumentGroup

class BootStrap {

    def init = { servletContext ->
        
        //loadDefaultUsers()
        loadDefaultData()

    }

    def loadDefaultUsers() {

        if (!(Role.count() == 0 && Account.count() == 0)) 
            return

        println "Loading default users and roles ..."
        
        def adminRole = new Role(authority: 'ROLE_ADMIN').save(flush: true)
        def userRole = new Role(authority: 'ROLE_USER').save(flush: true)

        def testUser = new Account(username: 'admin', password: 'admin') 
        testUser.save(flush: true)
        AccountRole.create testUser, adminRole, true

        assert Account.count() == 1 
        assert Role.count() == 2 
        assert AccountRole.count() == 1
    }

    def loadDefaultData() {

        addFileTypes()
        addDocumentTypes()
        addDocumentGroups()

    }

    def addFileTypes() {

        if (FileType.count() != 0) return

        println "Loading default file types ..."
 
        def addFileType = { shortName, name ->
            def fileType = new FileType(shortName: shortName, name: name)
            fileType.save(flush: true)
        }

        def fileTypesPath = "resources/fileTypes.csv"
        def fileTypes = ApplicationHolder.application.parentContext.
                            getResource("classpath:$fileTypesPath").
                            inputStream

        fileTypes.splitEachLine(",") { fields ->
            if (!fields[0].equals('shortName')) {
                addFileType(fields[0].trim(), fields[1].trim())
            }
        }

    }

    def addDocumentTypes() {

        if (DocumentType.count() != 0) return        

        println "Loading default document types ..."

        def getFileType = { fileTypeShortName ->
            FileType.findByShortName(fileTypeShortName)
        }

        def addDocumentType = { shortName, name, fileTypes, versionable, purgeable -> 
            def documentType = new DocumentType(shortName: shortName, 
                                                name: name,
                                                versionable: versionable,
                                                purgeable: purgeable,
                                                hasExpiration: purgeable,
                                                daysInAdvanceForNotice: 7,
                                                defaultLastingMonths: 12)
            fileTypes.each { fileTypeShortName ->      
                documentType.addToFileTypes(getFileType(fileTypeShortName))
            }
            documentType.save()
        }

        def documentTypesPath = "resources/documentTypes.csv"
        def documentTypes = ApplicationHolder.application.parentContext.
                            getResource("classpath:$documentTypesPath").
                            inputStream

        documentTypes.splitEachLine(",") { fields ->
            def fileTypes = fields[2].tokenize(':').collect { it.trim() }
            def versionable = fields[3].trim().toLowerCase() == 'true'
            def purgeable = fields[4].trim().toLowerCase() == 'true'
            if (!fields[0].equals('shortName')) {
                addDocumentType(fields[0].trim(), 
                                fields[1].trim(), 
                                fileTypes,
                                versionable,
                                purgeable)
            }
        }

    }

    def addDocumentGroups() {
        
        if (DocumentGroup.count() != 0) return

        println "Loading default document groups ..."

        def addDocumentGroup = { id, name -> 
            new DocumentGroup(id: id, name: name).save()
        }

        def documentGroupsPath = "resources/documentGroups.csv"
        def documentGroups = ApplicationHolder.application.parentContext.
                            getResource("classpath:$documentGroupsPath").
                            inputStream

        documentGroups.splitEachLine(",") { fields ->
            if (!fields[0].equals('id')) {
                addDocumentGroup(fields[0].trim().toLong(), fields[1].trim())
            }
        }
       
    }

    def destroy = {
    }

}
